package com.dycx.common;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class RestResponse<T> {
    //code：0-成功，-1-失败，-2-未登录，-3-未注册
    public Integer code;
    public String message;
    public T data;
    public LocalDateTime datetime;
    public String refreshToken;

    public RestResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.datetime = LocalDateTime.now();
    }

    public static <T> RestResponse<T> success() {
        return success("操作成功", null);
    }

    public static <T> RestResponse<T> success(T data) {
        return success("", data);
    }

    public static <T> RestResponse<T> success(String message, T data) {
        return message(0, message, data);
    }

    public static <T> RestResponse<T> fail() {
        return fail("操作失败", null);
    }

    public static <T> RestResponse<T> fail(String message) {
        return fail(message, null);
    }

    public static <T> RestResponse<T> fail(String message, T data) {
        return message(-1, message, data);
    }

    public static <T> RestResponse<T> notLogin(String message) {
        return message(-2, message, null);
    }

    public static <T> RestResponse<T> message(Integer code, String message, T data) {
        return new RestResponse<>(code, message, data);
    }

    public Boolean isSuccess() {
        return this.code == 0;
    }

}


